
//
//  HDStatusOriginlView.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/13.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit

class HDStatusOriginlView: UIView {
    //属性观察期给子控件赋值
    var statuses:HDStatus?{
        didSet{
            //1.头像
            if let profile_image_url = statuses?.user?.profileImageURL {
          
                //下载图片赋值
                iconView.sd_setImageWithURL( profile_image_url , placeholderImage: UIImage(named: "avatar_default"), completed: { (image, error, _, _) -> Void in
                    if  error != nil {
                        return
                    }
                    image.hd_AsyncDrawImage(nil, drawFinish: { (image) -> () in
                        self.iconView.image = image
                    })
                })
            }

   

            //2.昵称
            nameLabel.text = statuses?.user?.name
            //3.时间
            timeLabel.text =  NSDate.stringToDate(statuses?.created_at ?? "刚刚").sinaDatedescrition()
            //4.等级
            if let mbrankImage = statuses?.user?.mbrankImage{
                mbrankImage.hd_AsyncDrawImage(nil,drawFinish: { (image) -> () in
                    self.memberView.image = image
                })
            }else {
                self.memberView.image = nil
            }
          
            //5.认证图片
            statuses?.user?.verifiedtypeIamge?.hd_AsyncDrawImage(nil, drawFinish: { (image) -> () in
                self.verifiedView.image = image
            })
            //6.来源  暂时写死 TODO
            sourceLabel.text = statuses?.source
            //7.文字内容
            contentLabel.text = statuses?.text
            //8.给图片赋值
            pictureView.pictureURLs = statuses?.storePictureURLs
            //根据判断是否有图片 决定设置 文字到图片的距离
            pictureView.snp_makeConstraints { (make) -> Void in
                let margin = statuses?.storePictureURLs == nil ? 0 : cellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
            
        }
    }

//MARK -初始化方法
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame:CGRect){
        super.init(frame:frame)
        self.backgroundColor = UIColor.whiteColor()
        //创建子控件和添加约束
        prepareUI()
    }
    ///创建子控件和添加约束
    private func prepareUI(){
        //1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(memberView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //2.添加子控件的约束
        //2.1iconView
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(cellMargin)
            make.size.equalTo(CGSize(width:  iconViewWidth, height: iconViewHeight))
        }
        
        //2.2nameLabel
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(cellMargin)
            make.top.equalTo(iconView)
        }
        
        //2.3timeLabel
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        //2.4sourceLabel
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
            make.centerY.equalTo(timeLabel)
        }
        //2.5memberView
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //2.6verifiedView
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        //2.7contentLabel
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(cellMargin)
            make.trailing.equalTo(self).offset(-cellMargin)
        }
        //2.8pictureView
        pictureView.snp_makeConstraints { (make) -> Void in

            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.leading.equalTo(self).offset(cellMargin)
            //先设置为0  在pictureView根据图片计算更新实际高度
            make.size.equalTo(CGSizeZero)
        }

        //2.9设置view的底边距
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(cellMargin)
        }
        
    }

    
    //MARK  -懒加载子控件
    //1.用户头像  TODO  设置切圆
    private lazy var   iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    //2.用户名称
    private lazy var nameLabel :UILabel  = UILabel(color: UIColor.darkGrayColor(), fontsize: HDHomeNameLabelFontSize)
    //3.时间
    private lazy var timeLabel:UILabel  = UILabel(color: UIColor.orangeColor(), fontsize: HDHomeTimeLabelFontSize)
    //4.来源
    private lazy var sourceLabel:UILabel = UILabel(color: UIColor.lightGrayColor(), fontsize: HDHomeSourceLabelFontSize)
    //5.认证图标
    private lazy var verifiedView:UIImageView = UIImageView()
    //6.会员等级
    private lazy var memberView :UIImageView = UIImageView()
    //7.微博文本内容
    private lazy var contentLabel: UILabel = {
        let  label = UILabel(color: UIColor.darkGrayColor(), fontsize: HDHomeContentLabelFontSzie)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = ScreenW - 2*cellMargin
        return label
    }()
    //8.图片
    private lazy var  pictureView:HDPicturesView = HDPicturesView()
}
//相当于给UIlabel写分类
extension  UILabel {
    //遍历构造器
    convenience init(color: UIColor , fontsize:CGFloat , numberOflines : Int = 1){
        self.init()
        self.textColor = color
        self.font = UIFont.systemFontOfSize(fontsize)
        self.numberOfLines = numberOflines
    }
}
